package co.com.smart.login.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.SubmitButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginSmart extends Composite implements IsWidget{

	private static LoginSmartUiBinder uiBinder = GWT
			.create(LoginSmartUiBinder.class);

	interface LoginSmartUiBinder extends UiBinder<Widget, LoginSmart> {
	}

	@UiField
	protected SubmitButton boton;
	@UiField
	protected PasswordTextBox txtPass;
	@UiField 
	protected TextBox txtUser;
	@UiField
	protected Image botonAceptar;

	public LoginSmart() {
		initWidget(uiBinder.createAndBindUi(this));

		botonAceptar.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {			
				validar();
			}
		});


		KeyDownHandler key = new KeyDownHandler() {

			@Override
			public void onKeyDown(KeyDownEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER)
				{
					validar();

				}
			}
		};	
		
		txtPass.addKeyDownHandler(key);		
		txtUser.addKeyDownHandler(key);		
		
		Scheduler.get().scheduleDeferred(new Command() {
	        public void execute() {
	        	txtUser.setFocus(true);
	        }
	    });
	}
	
	private void validar()
	{
		if ("".equals(txtUser.getText()))
		{
			Window.alert("Ingrese el nombre de Usuario");
			Scheduler.get().scheduleDeferred(new Command() {
		        public void execute() {
		        	txtUser.setFocus(true);
		        }
		    });
		}
		else if ("".equals(txtPass.getText()))
		{
			Window.alert("Ingrese una contrase\u00f1a");
			Scheduler.get().scheduleDeferred(new Command() {
		        public void execute() {
		        	txtPass.setFocus(true);
		        }
		    });
		}
		else
		{				
			boton.setEnabled(true);				
			boton.click();
		}
	}


}
